package eu.ydp.mathplayer.editor;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.ui.Panel;

import eu.ydp.mathplayer.editor.controller.ModelManager;
import eu.ydp.mathplayer.editor.view.EditorView;

public class MathEditorManager {

	private JavaScriptObject jsObject;
	private EditorView view; 
	private ModelManager modelManager;
	
	public MathEditorManager(Panel owner, String source){
		jsObject = JavaScriptObject.createFunction();
		initJavaScriptAPI(jsObject);
		view = new EditorView();
		owner.add(view);
		
		modelManager = new ModelManager(source, view.getSandboxOwner());
		
		view.init(modelManager);
	}

	private native void initJavaScriptAPI(JavaScriptObject obj) /*-{
		var instance = this;
		obj.getMathML = function(){
			return instance.@eu.ydp.mathplayer.editor.MathEditorManager::getMathML()();
		}
		obj.setMathML = function(source){
			instance.@eu.ydp.mathplayer.editor.MathEditorManager::setMathML(Ljava/lang/String;)(source);
		}
	}-*/;
	
	public JavaScriptObject getJavaScriptObject(){
		return jsObject;
	}
	
	public String getMathML(){
		return modelManager.getMathML();
	}
	
	private void setMathML(String source){
		modelManager.processSource(source);
	}
}
